var express = require("express");
const { post, get } = require(".");
const { createToken } = require("tools-factory-tang/jwt");
const { login, reg, getAdmins } = require("../api/admins");
var router = express.Router();
//登录
router.post("/login", async function (req, res, next) {
  const { username, password } = req.body; //获取用户输入的用户名、密码(通过解构)
  const data = await login({ username, password });
  if (data) {
    const token = createToken({ username });
    res.send({
      status: true,
      username: data.username,
      token,
    });
  } else {
    res.send({
      status: false,
    });
  }
});
//注册
router.post("/reg", async function (req, res, next) {
  const { username, password } = req.body;
  const data = await reg({ username, password });
  if (data) {
    res.send({
      status: true,
    });
  } else {
    res.send({
      status: false,
    });
  }
});
//获取管理员数据
router.get("/", async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await getAdmins({ curPage, eachPage });
  res.send(data);
});
module.exports = router;
